#!/bin/sh -f

if [ $# = 1 ] ; then
   if [ "$1" = "-help" ] || [ "$1" = "-h" ] ; then
      echo "Usage: lefdefdiff -lef|-def inFilename1 inFilename2 [-o outFilename]"
      exit 1
   fi
fi

# minimum argument is 3
if [ $# -lt 3 ] ; then
   echo "Usage: lefdefdiff -lef|-def inFilename1 inFilename2 [-o outFilename]"
   exit 1
fi

# if has 5 arguments, the 4th has to be -o for the output filename
if [ $# = 5 ] ; then
   if [ "$4" != "-o" ] ; then
      echo "Usage: lefdefdiff -lef|-def inFilename1 inFilename2 [-o outFilename]"
      exit 1
   fi
fi

# it's a lef file
if [ "$1" = "-lef" ] || [ "$1" = "-LEF" ] ; then
   # 11/23/99 - Wanda da Rosa, PCR 284352, make the temporary files unique
   #            per process
   lefdiff $2 $3 /tmp/lefOut1.$$ /tmp/lefOut2.$$
   # 11/22/99 - Wanda da Rosa, PCR 281498, check the return status
   status=$?
   if [ $status != 0 ] ; then
      echo "Error is reading the input files, lefdefdiff stops execution!"
      exit $status
   fi
   if [ -f /tmp/lefOut1S.$$ ] ; then # if sort file for file 1 in tmp, remove it
      rm /tmp/lefOut1S.$$
   fi
   if [ -f /tmp/lefOut2S.$$ ] ; then # if sort file for file 2 in tmp, remove it
      rm /tmp/lefOut2S.$$
   fi
   if [ -f /tmp/lefOut1.$$ ] ; then  # if both output files are there, it means
      if [ -f /tmp/lefOut2.$$ ] ; then   # the parser parsed successfully
         sort /tmp/lefOut1.$$ > /tmp/lefOut1S.$$   # sort the output file
         sort /tmp/lefOut2.$$ > /tmp/lefOut2S.$$ 
         if [ $# = 5 ] ; then    # user has requested to save output is a file
            if [ -f $5 ] ; then # the file exist, remove it
               rm $5
            fi 
            echo "< $2" > $5
            echo "> $3" >> $5
            # -e #1 below:
            #    find something that matches begining of line, 1 or more numbers
            #    followed by 0 or 1 commas follow by 0 or more numbers
            #    followed by the letter 'c' ... and replace it with "Changed:"
            # -e #2 below:
            #    find something that matches begining of line, 1 or more numbers
            #    followed by 0 or 1 commas follow by 0 or more numbers
            #    followed by the letter 'd' ... and replace it with "Deleted:"
            # -e #3 below:
            #    find something that matches begining of line, 1 or more numbers
            #    followed by 0 or 1 commas follow by 0 or more numbers
            #    followed by the letter 'a' ... and replace it with "Added:"
            diff  /tmp/lefOut1S.$$ /tmp/lefOut2S.$$ | \
            sed \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*c[0-9][0-9]*,\{0,1\}[0-9]*/Changed:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*d[0-9][0-9]*,\{0,1\}[0-9]*/Deleted:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*a[0-9][0-9]*,\{0,1\}[0-9]*/Added:/' \
               >> $5
         else
            echo "< $2"
            echo "> $3"
            # no output file, output to standard out
            diff /tmp/lefOut1S.$$ /tmp/lefOut2S.$$ | \
            sed \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*c[0-9][0-9]*,\{0,1\}[0-9]*/Changed:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*d[0-9][0-9]*,\{0,1\}[0-9]*/Deleted:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*a[0-9][0-9]*,\{0,1\}[0-9]*/Added:/'
         fi
         rm /tmp/lefOut1.$$ /tmp/lefOut1S.$$ /tmp/lefOut2.$$ /tmp/lefOut2S.$$
      fi                       # remove all the temp files from /tmp
   fi
elif [ "$1" = "-def" ] || [ "$1" = "-DEF" ] ; then   # it's a def file
   defdiff $2 $3 /tmp/defOut1.$$ /tmp/defOut2.$$
   # 11/22/99 - Wanda da Rosa, PCR 281498, check the return status
   status=$?
   if [ $status != 0 ] ; then
      echo "Error is reading the input files, lefdefdiff stops execution!"
      exit $status
   fi
   if [ -f /tmp/defOut1S.$$ ] ; then # if sort file for file 1 in tmp, remove it
      rm /tmp/defOut1S.$$
   fi
   if [ -f /tmp/defOut2S.$$ ] ; then # if sort file for file 2 in tmp, remove it
      rm /tmp/defOut2S.$$
   fi
   if [ -f /tmp/defOut1.$$ ] ; then   # if both output files are there, it means
      if [ -f /tmp/defOut2.$$ ] ; then # the parser parsed successfully
         sort /tmp/defOut1.$$ > /tmp/defOut1S.$$   # sort the output file
         sort /tmp/defOut2.$$ > /tmp/defOut2S.$$
         if [ $# = 5 ] ; then       # user requested to save output is a file
            if [ -f $5 ] ; then     # the file exist, remove it
               rm $5
            fi
            echo "< $2" > $5
            echo "> $3" >> $5
            # -e #1 below:
            #    find something that matches begining of line, 1 or more
            #    numbers followed by 0 or 1 commas follow by 0 or more
            #    numbers followed by the letter 'c' ... and replace it 
            #    with "Changed:"
            # -e #2 below:
            #    find something that matches begining of line, 1 or more
            #    numbers followed by 0 or 1 commas follow by 0 or more
            #    numbers followed by the letter 'd' ... and replace it
            #    with "Deleted:"
            # -e #3 below:
            #    find something that matches begining of line, 1 or more
            #    numbers followed by 0 or 1 commas follow by 0 or more
            #    numbers followed by the letter 'a' ... and replace it
            #    with "Added:"
            diff  /tmp/defOut1S.$$ /tmp/defOut2S.$$ | \
            sed \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*c[0-9][0-9]*,\{0,1\}[0-9]*/Changed:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*d[0-9][0-9]*,\{0,1\}[0-9]*/Deleted:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*a[0-9][0-9]*,\{0,1\}[0-9]*/Added:/' \
               >> $5
         else
            echo "< $2"
            echo "> $3"
            # no output file, output to standard out
            diff /tmp/defOut1S.$$ /tmp/defOut2S.$$ | \
            sed \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*c[0-9][0-9]*,\{0,1\}[0-9]*/Changed:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*d[0-9][0-9]*,\{0,1\}[0-9]*/Deleted:/' \
               -e 's/^[0-9][0-9]*,\{0,1\}[0-9]*a[0-9][0-9]*,\{0,1\}[0-9]*/Added:/'
         fi
         rm /tmp/defOut1.$$ /tmp/defOut1S.$$ /tmp/defOut2.$$ /tmp/defOut2S.$$
      fi                       # remove all the temp files from /tmp
   fi
else
   # neither -lef nor -def
   echo "Usage: lefdefdiff -lef|-def inFilename1 inFilename2 [-o outFilename]"
fi
